<template>
  <el-dialog v-model="dialog.visible" title="核销券码" width="40%" @close="handleCloseDialog">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="150px"
      class="form"
      :disabled="!canEdit"
    >
      <el-form-item label="代金券券码" prop="couponCode">
        <el-input v-model.number.trim="formData.couponCode" placeholder="请输入代金券券码" />
      </el-form-item>
    </el-form>

    <template v-if="canEdit" #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="handleCloseDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { ElMessage } from "element-plus";
import couponApi from "@/api/coupon/coupon";

// 定义 emit，用于通知外部关闭事件
const emit = defineEmits(["update:modelValue", "loadData"]);
const dialog = reactive({
  title: "",
  visible: false,
});
const formRef = ref();
const loading = ref(false);

const formData = ref({
  linkId: undefined,
  couponCode: "",
});

const formRules = reactive({
  couponCode: [{ required: true, message: "请填写代金券券码", trigger: "blur" }],
});
async function handleSubmit() {
  const validate = await formRef.value.validate();
  if (!validate) return;
  try {
    await couponApi.audit(formData.value);
    ElMessage.success("成功");
    emit("loadData");
    handleCloseDialog();
  } catch (error) {
    console.log("error", error);
  }
}

// 关闭弹窗
function handleCloseDialog() {
  dialog.visible = false;
  formData.value.couponCode = "";
  emit("update:modelValue", false); // 关闭弹窗
}
const canEdit = ref(true);
function showModal(val, cannotEdit = false) {
  console.log(val.pageName);
  // MaterialBuy 是施工关联id Design是设计关联
  console.log(val.house);

  if (val.pageName == "MaterialBuy") {
    formData.value.linkId = val.house?.houseconstructionMaterialBuyvo?.id;
  } else if (val.pageName == "Design") {
    formData.value.linkId = val.house?.houseDesignLinkVO?.id;
  }
  canEdit.value = !cannotEdit;
  dialog.visible = true;

  console.log(formData.value);
}

defineExpose({
  showModal,
});
</script>
<style style="scss" scoped>
::v-deep(.el-tabs__content) {
  min-height: 100px;
  display: flex;
  align-items: center;
}
</style>
